package LandR.personal.personalServlet;


import LandR.personal.personalHelp.Users.User;
import LandR.personal.personalHelp.function_json.Util;
import LandR.personal.personalHelp.function_jsp.JspPageUseMethod;
import token.TokenHelp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;

@WebServlet("/PageServlet")
public class PageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPut(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPut(request, response);
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/json;charset=utf-8"); // 响应文本格式， json格式
        response.setCharacterEncoding("utf-8"); // 编码格式
        request.setCharacterEncoding("utf-8");

        if (request.getMethod().equals("PUT")) {

            //获取token
            String token = request.getHeader("token");
            token = token.substring(token.indexOf("\"") + 1, token.lastIndexOf("\""));
            System.out.println("前端传递的token令牌是：" + token);
            TokenHelp th = new TokenHelp();
            Integer ID = -1;

            try {
                ID = th.judgeToken(token);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

            if (ID != -1) {
                Util jsonHelp = new Util();
                //从前端传递的数据中读取信息
                InputStreamReader insr = new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8);
                StringBuilder result = new StringBuilder();
                int respInt = insr.read();
                while (respInt != -1) { // 读取请求数据
                    result.append((char) respInt);
                    respInt = insr.read();
                }
                System.out.println("前端传递的修改个人信息的数据为:" + result);
                insr.close();
                String data = result.toString();
                JspPageUseMethod jpum = new JspPageUseMethod();
                User user = new User();
                //将数据转化为User对象的格式
                user = jsonHelp.jsonToJavaObjectByFastJson(data);
                user.changeAge();
                user.changeSpecificAddress();
                user.setId(ID);
                System.out.println("---------数据接收成功");

                PrintWriter out = null;
                try {
                    jpum.updateUserData(user);
                    System.out.println("修改成功");
                    out = response.getWriter();
                    out.print("修改成功");
                    out.flush();
                } catch (SQLException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("失败！token已过期或token不匹配！");
                PrintWriter out = response.getWriter();
                out.print("失败！token已过期或token不匹配！");
            }

        } else {
            PrintWriter out = response.getWriter();
            System.out.println("失败！查询方法不匹配！");
            out.println("失败！查询方法不匹配！");

        }

    }
}
